package com.ybear.ybutils.utils.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;

import com.ybear.ybutils.utils.R;
import com.ybear.ybutils.utils.ResUtil;
import com.ybear.ybutils.utils.SysUtil;
import com.ybear.ybutils.utils.Utils;

public class DialogButton implements ButtonListener {
    private Context mContext;
    private Dialog mDialog;
    private String positiveText = null;
    private String negativeText = null;
    private TextStyle positiveStyle = new TextStyle();
    private TextStyle negativeStyle = new TextStyle();
    private DialogInterface.OnClickListener positiveClickListener, negativeClickListener;

    DialogButton(@NonNull Context context, Dialog dialog) {
        mContext = context;
        mDialog = dialog;
    }

    /**
     * 设置确定按钮点击事件监听器
     * @param positiveText  按钮文本内容
     * @param style         按钮样式
     * @param l             事件监听器
     * @return              this
     */
    @Override
    public DialogButton setOnPositiveListener(@Nullable String positiveText,
                                              @Nullable TextStyle style,
                                              @Nullable DialogInterface.OnClickListener l) {
        this.positiveText = positiveText == null ? getOKText() : positiveText;
        if( style != null ) positiveStyle = style;
        positiveClickListener = l;
        return this;
    }

    /**
     * 设置取消按钮点击事件监听器
     * @param negativeText  按钮文本内容
     * @param style         按钮样式
     * @param l             事件监听器
     * @return              this
     */
    @Override
    public DialogButton setOnNegativeListener(@Nullable String negativeText,
                                              @Nullable TextStyle style,
                                              @Nullable DialogInterface.OnClickListener l) {
        this.negativeText = negativeText == null ? getCancelText() : negativeText;
        if( style != null ) negativeStyle = style;
        negativeClickListener = l;
        return this;
    }

    /**
     * 设置确定按钮点击事件监听器
     * @param positiveText  按钮文本内容
     * @param style         按钮样式
     * @return              this
     */
    @Override
    public DialogButton setOnPositiveListener(@Nullable String positiveText,
                                              @Nullable TextStyle style) {
        this.positiveText = positiveText == null ? getOKText() : positiveText;
        if( style != null ) positiveStyle = style;
        positiveClickListener = (dialog, which) -> dialog.dismiss();
        return this;
    }

    /**
     * 设置取消按钮点击事件监听器
     * @param negativeText  按钮文本内容
     * @param style         按钮样式
     * @return              this
     */
    @Override
    public DialogButton setOnNegativeListener(@Nullable String negativeText,
                                              @Nullable TextStyle style) {
        this.negativeText = negativeText == null ? getCancelText() : negativeText;
        if( style != null ) negativeStyle = style;
        negativeClickListener = (dialog, which) -> dialog.dismiss();
        return this;
    }

    /**
     * 设置确定按钮点击事件监听器
     * @param positiveText   文本资源
     * @param l             事件监听器
     * @return              this
     */
    @Override
    public DialogButton setOnPositiveListener(@Nullable String positiveText,
                                              @Nullable DialogInterface.OnClickListener l) {
        return setOnPositiveListener(positiveText, null, l);
    }

    /**
     * 设置取消按钮点击事件监听器
     * @param negativeText  按钮文本内容
     * @param l             事件监听器
     * @return              this
     */
    @Override
    public DialogButton setOnNegativeListener(@Nullable String negativeText,
                                              @Nullable DialogInterface.OnClickListener l) {
        return setOnNegativeListener(negativeText, null, l);
    }

    /**
     * 设置确定按钮点击事件监听器
     * @param positiveText   文本资源
     * @return              this
     */
    @Override
    public DialogButton setOnPositiveListener(@Nullable String positiveText) {
        return setOnPositiveListener(positiveText, (TextStyle) null);
    }

    /**
     * 设置取消按钮点击事件监听器
     * @param negativeText  按钮文本内容
     * @return              this
     */
    @Override
    public DialogButton setOnNegativeListener(@Nullable String negativeText) {
        return setOnNegativeListener(negativeText, (TextStyle) null);
    }

    /**
     * 设置确定按钮点击事件监听器
     * @param positiveRes   文本资源
     * @param style         按钮样式
     * @param l             事件监听器
     * @return              this
     */
    @Override
    public DialogButton setOnPositiveListener(@StringRes int positiveRes,
                                              @Nullable TextStyle style,
                                              @Nullable DialogInterface.OnClickListener l) {
        return setOnPositiveListener(mContext != null ? getString( positiveRes ) : null, style, l);
    }

    /**
     * 设置取消按钮点击事件监听器
     * @param negativeRes   文本资源
     * @param style         按钮样式
     * @param l             事件监听器
     * @return              this
     */
    @Override
    public DialogButton setOnNegativeListener(@StringRes int negativeRes,
                                              @Nullable TextStyle style,
                                              @Nullable DialogInterface.OnClickListener l) {
        return setOnNegativeListener(mContext != null ? getString( negativeRes ) : null, style, l);
    }

    /**
     * 设置确定按钮点击事件监听器
     * @param positiveRes   文本资源
     * @param style         按钮样式
     * @return              this
     */
    @Override
    public DialogButton setOnPositiveListener(@StringRes int positiveRes,
                                              @Nullable TextStyle style) {
        return setOnPositiveListener(mContext != null ? getString( positiveRes ) : null, style);
    }

    /**
     * 设置取消按钮点击事件监听器
     * @param negativeRes   文本资源
     * @param style         按钮样式
     * @return              this
     */
    @Override
    public DialogButton setOnNegativeListener(@StringRes int negativeRes,
                                              @Nullable TextStyle style) {
        return setOnNegativeListener(mContext != null ? getString( negativeRes ) : null, style);
    }

    /**
     * 设置确定按钮点击事件监听器
     * @param positiveRes   文本资源
     * @param l             事件监听器
     * @return              this
     */
    @Override
    public DialogButton setOnPositiveListener(@StringRes int positiveRes,
                                              @Nullable DialogInterface.OnClickListener l) {
        return setOnPositiveListener(positiveRes, null, l);
    }

    /**
     * 设置取消按钮点击事件监听器
     * @param negativeRes   文本资源
     * @param l             事件监听器
     * @return              this
     */
    @Override
    public DialogButton setOnNegativeListener(@StringRes int negativeRes,
                                              @Nullable DialogInterface.OnClickListener l) {
        return setOnNegativeListener(negativeRes, null, l);
    }

    /**
     * 设置确定按钮点击事件监听器
     * @param positiveRes   文本资源
     * @return              this
     */
    @Override
    public DialogButton setOnPositiveListener(@StringRes int positiveRes) {
        return setOnPositiveListener(positiveRes, (TextStyle) null);
    }

    /**
     * 设置取消按钮点击事件监听器
     * @param negativeRes   文本资源
     * @return              this
     */
    @Override
    public DialogButton setOnNegativeListener(@StringRes int negativeRes) {
        return setOnNegativeListener(negativeRes, (TextStyle) null);
    }

    /**
     * 设置确定按钮点击事件监听器
     * @param style         按钮样式
     * @param l             事件监听器
     * @return              this
     */
    @Override
    public DialogButton setOnPositiveListener(@Nullable TextStyle style,
                                              @Nullable DialogInterface.OnClickListener l) {
        return setOnPositiveListener(null, style, l);
    }

    /**
     * 设置取消按钮点击事件监听器
     * @param style         按钮样式
     * @param l             事件监听器
     * @return              this
     */
    @Override
    public DialogButton setOnNegativeListener(@Nullable TextStyle style,
                                              @Nullable DialogInterface.OnClickListener l) {
        return setOnNegativeListener(null, style, l);
    }

    /**
     * 设置确定按钮点击事件监听器
     * @param l             事件监听器
     * @return              this
     */
    @Override
    public DialogButton setOnPositiveListener(@Nullable DialogInterface.OnClickListener l) {
        return setOnPositiveListener(null, null, l);
    }

    /**
     * 设置取消按钮点击事件监听器
     * @param l             事件监听器
     * @return              this
     */
    @Override
    public DialogButton setOnNegativeListener(@Nullable DialogInterface.OnClickListener l) {
        return setOnNegativeListener(null, null, l);
    }

    @Nullable
    View createButton() {
        if( mContext == null ) return null;

        LinearLayout layout = new LinearLayout( mContext );
        layout.setLayoutParams(new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT)
        );
        layout.setPadding(
                Utils.dp2Px(mContext, DialogInit.get().getButtonLayoutLeftPadding()),
                Utils.dp2Px(mContext, DialogInit.get().getButtonLayoutTopPadding()),
                Utils.dp2Px(mContext, DialogInit.get().getButtonLayoutRightPadding()),
                Utils.dp2Px(mContext, DialogInit.get().getButtonLayoutBottomPadding())
        );
        layout.setGravity( Gravity.TOP | Gravity.END );
        //按钮的布局参数
        LinearLayout.LayoutParams btnLP = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        if( positiveText != null ) {
            TextView v = positiveStyle
                    .setPadding( Utils.dp2Px(mContext, DialogInit.get().getButtonPadding()) )
                    .setOnClickListener(v1 -> {
                        //回调确定按钮事件监听器
                        if( positiveClickListener == null ) return;
                        positiveClickListener.onClick(mDialog, DialogInterface.BUTTON_POSITIVE);
                    })
                    .createView( mContext, positiveText );
            btnLP.rightMargin = Utils.dp2Px(mContext, DialogInit.get().getButtonRightMargin());
            v.setLayoutParams( btnLP );
            v.setBackgroundResource(R.drawable.selector_def_btn);
            v.setFocusable( true );
            v.setClickable( true );
            layout.addView( v );
        }
        if( negativeText != null ) {
            TextView v = negativeStyle
                    .setPadding( Utils.dp2Px(mContext, DialogInit.get().getButtonPadding()) )
                    .setOnClickListener(v1 -> {
                        //回调取消按钮事件监听器
                        if( negativeClickListener == null ) return;
                        negativeClickListener.onClick(mDialog, DialogInterface.BUTTON_NEGATIVE);
                    })
                    .createView( mContext, negativeText );
            btnLP.rightMargin = Utils.dp2Px(mContext, DialogInit.get().getButtonRightMargin());
            v.setLayoutParams( btnLP );
            v.setBackgroundResource(R.drawable.selector_def_btn);
            v.setFocusable( true );
            v.setClickable( true );
            layout.addView( v );
        }
        return layout;
    }

    private String getString(int resId) { return ResUtil.getString( mContext, resId ); }

    private boolean isZH() { return "zh".equals( SysUtil.getLanguage() ); }

    private String getOKText() { return isZH() ? "确定" : "OK"; }

    private String getCancelText() { return isZH() ? "取消" : "Cancel"; }

}